import * as index from '@/api/index.js'
import {
	baseURL
} from '@/common/config.js'			
export default {
	data() {
		return {
			form: {
				id: '',
				head_image:'',
				shop_image:'',
				name: '', //门店名称
				head: '', //门店负责人
				mobile: '', //门店手机
				address: '', //地址
				latitude: '',
				longitude: '',
				province: '',
				city: '',
				area: ''
			},
			id: 0,
			latitude: 39.909,
			longitude: 116.39742,
			covers: [],
			checked: false,
			rules: {
				// 对name字段进行必填验证
				head_image: {
					rules: [{
						required: true,
						errorMessage: '请输入门店照片',
					}]
				},
				shop_image: {
					rules: [{
						required: true,
						errorMessage: '请输入营业执照',
					}]
				},
				name: {
					rules: [{
						required: true,
						errorMessage: '请输入门店名称',
					}]
				},
				head: {
					rules: [{
						required: true,
						errorMessage: '请输入门店负责人',
					}]
				},
				mobile: {
					rules: [{
						required: true,
						errorMessage: '请输入门店手机',
					}]
				},
				address: {
					rules: [{
						required: true,
						errorMessage: '请选择门店地址',
					}]
				},
			},
			shopData: '',
			shopLoading: false,
			statusInfo: {
				status: '',
				refuse: ''
			}
		}
	},

	onShow() {

	},
	onLoad(option) {
		this.getMyShop()
		let that = this
		uni.getLocation({
			type: 'gcj02', //返回可以用于uni.openLocation的经纬度
			success: function(res) {
				that.latitude = res.latitude;
				that.longitude = res.longitude;
				// that.covers[0] = {
				// 	id:0,
				// 	latitude: 39.909,
				// 	longitude: 116.39742,
				// }
			},
		});
	},
	onReady() {
		// 需要在onReady中设置规则
		this.$refs.form.setRules(this.rules)
	},
	methods: {
		submit() {
		
		},
		close() {
			this.$refs.popup.close()
		},
		radioClick() {
			this.checked = !this.checked
		},
		goWebview(item) {
			this.$tools.navigate('/pages/user/webview/index?name=' + item)
		},
		mapclick() {
			let that = this
			uni.chooseLocation({
				success: function(res) {
					console.log(res, 'res');
					that.form.address = res.address
					that.form.latitude = res.latitude
					that.form.longitude = res.longitude
					// that.form.province = res.province
					// that.form.city = res.city
					// that.form.area = res.area
					that.latitude = res.latitude;
					that.longitude = res.longitude;
					that.$myAmapFun.getRegeo({
						location: `${res.longitude},${res.latitude}`,
						success: function(data) {
							console.log(data, '解析');
							// that.form.address = data[0].desc;
							that.form.province = data[0].regeocodeData.addressComponent.province
							that.form.city = data[0].regeocodeData.addressComponent.city;
							that.form.area = data[0].regeocodeData.addressComponent.district;
							// // city:[]
							// if(Object.prototype.toString.call(city)=="[object Array]"){
							//   city = city.join('');
							// }

						},
						fail: function(info) {
							//失败回调
						}
					})
				}
			});

		},
		submit() {
			if (this.checked) {
				this.$refs.form.validate().then(res => {
					index.editShop(this.form).then(res => {
						console.log(res, 'res');
						// this.close()
						// this.$tools.toast(res.msg)
						// this.getMyShop()
						this.$tools.back(1)
					})
				}).catch(err => {
					console.log('表单错误信息：', err);
				})
				
			} else {
				this.$tools.toast('请先阅读并同意《服务规范》')
			}
		},
		getMyShop() {	
			index.getMyShop().then(res => {
				console.log(res, '我的门店');
				this.shopLoading = true
				if (res.data) {
					this.form.shop_image = res.data.shop_image
					this.form.head_image = res.data.head_image
					this.form.id = res.data.id
					this.shopData = res.data
					this.form.name = res.data.name
					this.form.head = res.data.head
					this.form.mobile = res.data.mobile
					this.form.address = res.data.address
					this.form.latitude = res.data.latitude
					this.form.longitude = res.data.longitude
					this.form.province = res.data.province
					this.form.city = res.data.city
					this.form.area = res.data.area
					this.form.shop_image = res.data.shop_image
					this.form.head_image = res.data.head_image
					this.statusInfo.status = res.data.check_status
					this.statusInfo.refuse = res.data.refuse ? res.data.refuse : ''
				}

			})
		},
		edit() {
			this.$refs.popup.open('center')
		},
		uploadImage(item){
			let that = this
			if(item==1){
				uni.chooseMedia({
					count: 1,
					mediaType: ['image'],
					sourceType: ['album', 'camera'],
					maxDuration: 30,
					camera: 'back',
					success(res) {
						console.log(res,'res');
						uni.uploadFile({
							url: baseURL + 'common/upload', //仅为示例，非真实的接口地址
							filePath: res.tempFiles[0].tempFilePath,
							name: 'file',
							formData: {
								'user': 'test',
								'Content-type': 'application/json',
								'token': uni.getStorageSync('token'),
							},
							success(images) {
								console.log(images)
								that.form.head_image  = JSON.parse(images.data).data.fullurl
								//do something
							}
						})
				
					}
				})
			}
			else if(item==2){
				uni.chooseMedia({
					count: 1,
					mediaType: ['image'],
					sourceType: ['album', 'camera'],
					maxDuration: 30,
					camera: 'back',
					success(res) {
						console.log(res,'res');
						uni.uploadFile({
							url: baseURL + 'common/upload', //仅为示例，非真实的接口地址
							filePath: res.tempFiles[0].tempFilePath,
							name: 'file',
							formData: {
								'user': 'test',
								'Content-type': 'application/json',
								'token': uni.getStorageSync('token'),
							},
							success(images) {
								console.log(images)
								that.form.shop_image  = JSON.parse(images.data).data.fullurl
								//do something
							}
						})
				
					}
				})
			}
			
		}
	}
}